"""
网易云音乐排行榜歌曲信息抓取
"""
from selenium import webdriver

# 1.打开浏览器，进入排行榜首页
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get(url='https://music.163.com/#/discover/toplist')

# 2.切换iframe子页面,只支持id和name的切换
driver.switch_to.frame('contentFrame')

# 3.提数据
tr_list = driver.find_elements_by_xpath('//table/tbody/tr')
for tr in tr_list:
    item = {}
    item['rank'] = tr.find_element_by_class_name("num").text
    item['title'] = tr.find_element_by_xpath('.//span[@class="txt"]/a/b').get_attribute('title').replace('\xa0', ' ')
    item['time'] = tr.find_element_by_xpath('.//span[@class="u-dur "]').text
    item['singer'] = tr.find_element_by_class_name("text").get_attribute('title')
    print(item)




driver.quit()






















